//
//  NSDictionary+Extension.m
//  SYRJ
//
//  Created by cooptec on 2018/5/2.
//  Copyright © 2018年 Law. All rights reserved.
//

#import "NSDictionary+Extension.h"

@implementation NSDictionary (Extension)
+ (NSString *)returnToJsonData:(id)dict {
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString;
    if (!jsonData) {
        NSLog(@"%@",error);
    }
    else {
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    return mutStr;
}

+ (NSDictionary *)returnToDictionaryWithStr:(NSString *)jsonStr {
    NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];
    if (!error) {
        return dic;
    }
    return @{};
}

- (NSString *)safeStringForKey:(NSString *)key {
    id value = [self valueForKey:key defaultsTo:@""];
    if ([value isKindOfClass:NSString.class]) {
        return value;
    }
    else {
        return [NSString stringWithFormat:@"%@",value];
    }
}

- (NSInteger)safeIntegerForKey:(NSString *)key {
    id value = [self valueForKey:key defaultsTo:nil];
    return (!value || value == NSNull.null) ? 0 : [value integerValue];
}

- (CGFloat)safeFloatForKey:(NSString *)key {
    id value = [self valueForKey:key defaultsTo:nil];
    return (!value || value == NSNull.null) ? 0 : [value floatValue];
}

- (int)safeIntForKey:(NSString *)key {
    id value = [self valueForKey:key defaultsTo:nil];
    return (!value || value == NSNull.null) ? 0 : [value intValue];
}

- (BOOL)safeBoolForKey:(NSString *)key {
    id value = [self valueForKey:key defaultsTo:nil];
    return (!value || value == NSNull.null) ? NO : [value boolValue];
}

- (NSDate *)safeDateForKey:(NSString *)key {
    id value = [self valueForKey:key defaultsTo:nil];
    return ([value isKindOfClass:NSDate.class]) ? value : [self.class _parseDate:value];
}

+ (NSDate *)_parseDate:(NSNumber *)number {
    if ([number isKindOfClass:NSNull.class] || number == nil) return nil;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[number doubleValue]];
    return date;
}

- (NSArray *)safeArrayForKey:(NSString *)key {
    id value = [self valueForKey:key defaultsTo:nil];
    return ([value isKindOfClass:NSArray.class]) ? value : nil;
}

- (id)valueForKey:(NSString *)key defaultsTo:(id)defaultValue {
    id value = [self valueForKey:key];
    return (value != nil && value != NSNull.null) ? value : defaultValue;
}

@end
